<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_Model_Photo extends Core_Model_Item_Collectible {
	protected $_parent_type = 'cmspage_album';
	
	protected $_owner_type = 'user';
	
	protected $_collection_type = 'cmspage_album';
	
	public function getHref($params = array()) {
		$params = array_merge ( array ('route' => 'cmspage_extended', 'reset' => true, 'controller' => 'photo', 'action' => 'view', 'cmspage_id' => $this->getCollection ()->getOwner ()->getIdentity (), //'album_id' => $this->collection_id,
		'photo_id' => $this->getIdentity () ), $params );
		$route = $params ['route'];
		$reset = $params ['reset'];
		unset ( $params ['route'] );
		unset ( $params ['reset'] );
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( $params, $route, $reset );
	}
	
	public function getPhotoUrl($type = null) {
		if (empty ( $this->file_id )) {
			return null;
		}
		
		$file = $this->api ()->getApi ( 'storage', 'storage' )->get ( $this->file_id, $type );
		if (! $file) {
			return null;
		}
		
		return $file->map ();
	}
	
	public function getCmspage() {
		return Engine_Api::_ ()->getItem ( 'cmspage', $this->cmspage_id );
	
		//return $this->getCollection()->getGroup();
	}
	
	public function isSearchable() {
		$collection = $this->getCollection ();
		if (! $collection instanceof Core_Model_Item_Abstract) {
			return false;
		}
		return $collection->isSearchable ();
	}
	
	public function getAuthorizationItem() {
		return $this->getParent ( 'cmspage' );
	}
	
	/**
	 * Gets a proxy object for the comment handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function comments() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'comments', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the like handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function likes() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'likes', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the tags handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function tags() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'tags', 'core' ) );
	}
	
	protected function _postDelete() {
		if ($this->_disableHooks)
			return;
		
		// This is dangerous, what if something throws an exception in postDelete
		// after the files are deleted?
		try {
			$file = $this->api ()->getApi ( 'storage', 'storage' )->get ( $this->file_id );
			if ($file) {
				$file->remove ();
			}
			
			$types = array ('thumb.profile', 'thumb.normal', 'thumb.icon' );
			foreach ( $types as $type ) {
				$file = $this->api ()->getApi ( 'storage', 'storage' )->get ( $this->file_id, $type );
				if ($file) {
					$file->remove ();
				}
			}
			
			$album = $this->getCollection ();
			
			if (( int ) $album->photo_id == ( int ) $this->getIdentity ()) {
				$album->photo_id = $this->getNextCollectible ()->getIdentity ();
				$album->save ();
			}
		} catch ( Exception $e ) {
			// @todo completely silencing them probably isn't good enough
		//throw $e;
		}
	}
	
	public function canEdit($user) {
		return $this->getParent ()->getParent ()->authorization ()->isAllowed ( $user, 'edit' ) || $this->isOwner ( $user );
	}
}